package com.suray.attendance.controller;


import com.suray.attendance.common.dingding.service.DingAccessTokenService;
import com.suray.attendance.common.result.Result;
import com.suray.attendance.common.utils.LoggerUtil;
import com.suray.attendance.contacts.service.DingUserService;
import com.suray.attendance.db.service.DingDeptDBService;
import com.suray.attendance.db.service.DingUserDBService;
import com.suray.attendance.model.pojo.UserInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: jianglinyu
 * @CreateTime: 2023-07-05  15:11
 * @Description:  钉钉用户的信息
 * @Version: 1.0
 */
@CrossOrigin
@RequestMapping("/api/dingUser")
@RestController
@CommonsLog
public class DingUserController {

    @Resource
    private DingUserService dingUserService;
    @Resource
    private DingUserDBService dingUserDBService;
    @Resource
    private DingDeptDBService dingDeptDBService;
    @Resource
    private DingAccessTokenService dingAccessTokenService;
    /**
     * @description:  根据用户id 获取用户钉钉详细信息
     * @author: jianglinyu
     * @date: 2023/7/5 15:18
     * @param: [userId]
     * @return: java.lang.String
     **/
//    @Async("task1")
//    @Scheduled(cron = "* * 4 ? * 6")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation(value = "每周六4点先进行表删除再从钉钉批量新增或者更新用户数据到数据库")
    @PostMapping(value = "/insertOrUpdateUserInFromDingDingToDB")
    public Result insertOrUpdateUserInFromDingDingToDB(){
        try {
            // 删除数据库
            Boolean deleteSuccess = dingUserDBService.deleteAll();
            if(deleteSuccess){
                String accessToken = dingAccessTokenService.getAccessToken();
                // 1. 从deptinfo里拿去部门id (根部门另外拿)
                List<Long> deptIdListFromDB = dingDeptDBService.getDeptIdListFromDB();
                deptIdListFromDB.stream().forEach(id->{
                    try {
                        List<UserInfo> userInfoList = dingUserService.getUserInfoFromDingDingByDeptId(accessToken,id);
                        dingUserDBService.insertOrUpdateToDB(userInfoList);
                    } catch (Exception e) {
                        e.printStackTrace();
                        LoggerUtil.error("从钉钉根据部门="+id+"批量新增或者更新用户数据到DB异常"+e.getMessage(),DingUserController.class);
                        throw new RuntimeException("从钉钉根据部门="+id+"批量新增或者更新用户数据到DB异常"+e.getMessage());
                    }
                });
                return Result.ok();
            }
        } catch (Exception e) {
            e.printStackTrace();
            LoggerUtil.error("批量新增或者更新用户数据到DB异常"+e.getMessage(),DingUserController.class);
            throw new RuntimeException("批量新增或者更新用户数据到DB异常"+e.getMessage());
        }
        return Result.fail();
    }


    @ApiOperation(value = "根据用户名获取唯一的用户信息（部门唯一）")
    @GetMapping("/getUserInfoByName")
    public Result getUserInfoByName(String name){
        LoggerUtil.info("根据用户名"+name+"获取唯一的用户信息（部门唯一）",DingUserController.class);
        UserInfo userInfo = dingUserDBService.getUserInfoByName(name);
        System.out.println(userInfo);
        return Result.ok(userInfo);
    }
}
